/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： UserInterceptor.java
 * Date：18-11-5 上午11:10
 * Author: guomw
 */

package com.guomw.report.web.config;

import com.guomw.report.service.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户页面访问拦截器
 * @author guomw
 * @date 2018/9/21
 */
public class UserInterceptor implements HandlerInterceptor {

    @Autowired
    private SystemService systemService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        Integer operatorId = null;
        if (request.getSession().getAttribute("OwnerId") != null) {
            operatorId = Integer.valueOf(request.getSession().getAttribute("OwnerId").toString());
        }
        boolean checked = false;
        if (operatorId != null && operatorId > 0) {
            if ("/home".equals(request.getServletPath())) {
                checked = true;
            } else {
                checked = systemService.checkOperatorPathPower(operatorId, request,false);
                if(!checked)
                {
                    response.sendRedirect("/bank");
                    return false;
                }
            }
        }
        if (!checked) {
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
